home *** CD-ROM | disk | FTP | other *** search
- // vertex shader to combine shadow map texture with model drawing to create shadows
- // this shader is used when drawing objects.
-
- vs_1_1 // version
- dcl_position v0 // position
- dcl_normal v3 // normal
- // dcl_color v6 // vertex color
- dcl_texcoord v7 // texture coordinates
-
- def c21,2,-1,-10,0 // for calculating appropriate zbias
- def c22,0.5,0.5,0.5,0.5 // used to go from -1 to 1 to 0 to 1
-
- // constants supplied by app:
- // c0-c3: MVP matrix
- // c4: material diffuse color
- // c5: material ambient color
- // c6: shadow color (r,g,b,a)
- // c7: fogend,1/(fogend-fogstart),zbias,zfactor
- // c8-c11: light MVP matrix
- // c12-c15: light MVP matrix * texture adjustment matrix
- // c16: global ambient color
- // c17: sunlight direction
- // c18: sunlight color
- // c19: moonlight direction
- // c20: moonlight color
-
- // calculate position
- m4x4 r0, v0, c0
- mov oPos,r0
-
- // oFog = (fogend - dist) / (fogend-fogstart)
- sub r1.x,c7.x,r0.w
- mul oFog,r1.x,c7.y
-
- mov oT0,v7 // move the texture coordinate to output
-
- m4x4 r0,v0, c12 // compute shadow map texture coordinates
- mov oT1,r0
-
- dp3 r0,v3,c17 // compute dot product to get intensity
- m4x4 r1,v0, c8 // compute position from point of view of light (just want z from this calc for comparison in pixel shader)
-
- slt r1.x,r0.x,c21.w // r1.x = facing light ? 0 : 1
- mul r1.x,r1.x,c21.z // r1.x = - 10 * r1.x
- add r1.x,r1.x,c7.z // add zbias (if facing light, zbias, else -10 + zbias so that faces not facing light dont get shadows)
-
- mad r0,r0,c22,c22 // range from 0 to 1 instead of -1 to 1
- mad oT2.x, r1.z,c7.w,r1.x // t1.z = the z-value we are going to compare with the texture pixel
-
- dp3 r2,v3,c19 // moon direction
- mad r2,r2,c22,c22 // range from 0 to 1 instead of -1 to 1
- mul r2,r2,c20 // multiply by moonlight color
- mul r2,r2,c4 // multiply by material color
-
- mov r0.a,c5.a // ambient a was set to 1.0, we don't want light calculation to modify material alpha
- mul r1,c4,r0.x // r1 = material color * lightnormal * direction
- mul r1,r1,c18 // multiply by sunlight color
-
- add r1.rgb,r1.rgb,r2.rgb // add moonlight color
-
- add r0,c16,r1 // add global ambient color
- mov oD0,r0 // pass the resulting color along to the pixel shader
-